package com.vegetable.modules.entity.stock;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.Version;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.vegetable.common.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存表
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-21
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="Stock对象", description="库存表")
public class Stock extends BaseEntity {

    @ApiModelProperty(value = "商品ID")
    private Long commodityId;

    @ApiModelProperty(value = "商品名称")
    private String commodityName;

    @ApiModelProperty(value = "商品编码")
    private String commonCode;

    @ApiModelProperty(value = "商品类型ID")
    private Long commodityTypeId;

    @ApiModelProperty(value = "商品类型名称")
    private String category;

    @ApiModelProperty(value = "订单时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd")
    private Date orderTime;

    private Date createTime;

    @ApiModelProperty(value = "规格ID")
    private Long specsId;

    @ApiModelProperty(value = "规格名称")
    private String specsName;

    @ApiModelProperty(value = "商品标签(0原料，1半成品，2成品，3材料)")
    private Integer tag;

    @ApiModelProperty(value = "标准")
    private BigDecimal standard;

    @ApiModelProperty(value = "数量")
    private Integer number;

    @ApiModelProperty(value = "初始数量")
    private Integer initNumber;

    @ApiModelProperty(value = "重量")
    private BigDecimal weight;

    @ApiModelProperty(value = "初始重量")
    private BigDecimal initWeight;

    @ApiModelProperty(value = "成本：蔬菜 + 包装 + 其他费用（比如运费/小工等等）")
    private BigDecimal cost;

    @ApiModelProperty(value = "每公斤成本价格")
    private BigDecimal costWeight;

    @ApiModelProperty(value = "入库类型：0 代办交货 1 物流园 2 散货采购")
    private Integer storageType;

    @ApiModelProperty(value = "单据编号")
    private String storageOrderNo;

    @ApiModelProperty(value = "仓库id")
    private Long warehouseId;

    @ApiModelProperty(value = "仓库名称")
    private String warehouseName;

    @ApiModelProperty(value = "库存批次编码")
    private String stockBatchNo;

    @ApiModelProperty(value = "商品来源")
    private String agentName;

    @ApiModelProperty(value = "包装成本")
    private String packageCost;

    @ApiModelProperty(value = "包装成本-销售显示")
    @TableField(exist = false)
    private double packageCostForSales;

    @ApiModelProperty(value = "蔬菜单价")
    private BigDecimal goodsUnitPrice;

    @ApiModelProperty(value = "包装单价（按重均摊）")
    private BigDecimal packageUnitPrice;

    @ApiModelProperty(value = "库内管理费用单价（按重均摊）")
    private BigDecimal wmsUnitPrice;

    @ApiModelProperty(value = "订单id")
    private Long orderId;

    @ApiModelProperty(value = "详情id")
    private Long detailId;

    @Version
    @ApiModelProperty(value = "版本號")
    private Integer version;
    
    /**
     * 获取库存的完整描述信息
     * @return 库存描述
     */
    public String getFullDescription() {
        return String.format("%s(%s) - 批次:%s 仓库:%s", 
            this.commodityName, 
            this.specsName, 
            this.stockBatchNo, 
            this.warehouseName);
    }
    
    /**
     * 获取库存的价值信息
     * @return 库存价值描述
     */
    public String getValueDescription() {
        if (this.weight != null && this.costWeight != null) {
            BigDecimal totalValue = this.weight.multiply(this.costWeight);
            return String.format("重量:%s公斤 成本单价:%s元 总价值:%s元", 
                this.weight, 
                this.costWeight, 
                totalValue);
        }
        return "价值信息不完整";
    }
    
    /**
     * 获取库存批次号的基础部分（去除+1,+2等后缀）
     * @return 批次号基础部分
     */
    public String getBaseBatchNo() {
        if (this.stockBatchNo == null) {
            return null;
        }
        
        int plusIndex = this.stockBatchNo.indexOf('+');
        if (plusIndex > 0) {
            return this.stockBatchNo.substring(0, plusIndex);
        }
        return this.stockBatchNo;
    }
    
    /**
     * 获取与当前库存相关的分摊记录
     * 用于在其他因素介入时能够根据历史分摊信息正确计算当前的成本价格
     * @return 分摊记录列表
     */
    @TableField(exist = false)
    private List<CostAllocationRecord> allocationRecords;
    
    /**
     * 获取与当前库存相关的分摊记录
     * 用于在其他因素介入时能够根据历史分摊信息正确计算当前的成本价格
     * @return 分摊记录列表
     */
    public List<CostAllocationRecord> getAllocationRecords() {
        return allocationRecords;
    }
    
    /**
     * 设置与当前库存相关的分摊记录
     * @param allocationRecords 分摊记录列表
     */
    public void setAllocationRecords(List<CostAllocationRecord> allocationRecords) {
        this.allocationRecords = allocationRecords;
    }
}